package service

import (
	"fmt"
	ClickHouse "gin/database"
	"gin/model"
	"gin/types"
	"time"
)

type DingTalk struct {
}

// GetDepartmentUserManhourM 查询部门用户钉钉工时
func (d DingTalk) GetDepartmentUserManhourM(tenantId int64, from time.Time, to time.Time) (res map[string]types.DepartmentUserDingManhourM, err error) {
	res = make(map[string]types.DepartmentUserDingManhourM, 0)
	var manhourList []types.DepartmentUserDingManhourM
	err = ClickHouse.DB.Model(&model.TenantUser{}).Joins("JOIN tenant_ding_talk_attendance_manhour on tenant_ding_talk_attendance_manhour.user_id = tenant_user.user_id").
		Where("tenant_user.tenant_id = ? AND tenant_ding_talk_attendance_manhour.tenant_id = ?", tenantId, tenantId).
		Where("tenant_ding_talk_attendance_manhour.work_date BETWEEN ? AND ?", from.Unix(), to.Unix()).
		Group("tenant_user.department_id,tenant_user.user_id").
		Select("tenant_user.department_id as department_id,tenant_user.user_id as user_id,sum(tenant_ding_talk_attendance_manhour.manhour_m) as man_hour_m").
		Find(&manhourList).Error
	if err != nil {
		return
	}
	for _, m := range manhourList {
		res[fmt.Sprintf("%d#%d", m.DepartmentId, m.UserId)] = m
	}

	return
}
